package com.epro.dx.task;

import java.util.ArrayList;
import java.util.List;

import com.epro.dx.listenner.MyAsyncTaskListenner;
import com.epro.dx.util.ActivityUtil;
import com.epro.dx.util.OnekeyFilterLocalUtil;
import com.weipu.common.constants.ServerConstant;
import com.weipu.common.facade.exception.DxException;
import com.weipu.common.facade.factory.ProviderFactory;
import com.weipu.common.facade.model.CatelogModel;
import com.weipu.common.facade.model.OneKeyFilterResponseModel;
import com.weipu.common.util.StringUtil;

import android.content.Context;
import android.os.AsyncTask;

/************************
 * @author Tony
 * @Date:2016-3-29下午3:33:10
 * @描述:获取用户一键过滤的设置信息的任务
 ************************/
public class GetOneKeyFilterSettingsAsyncTask extends BaseNeedReLoginTask {

	private MyAsyncTaskListenner myAsyncTaskListenner;
	private OneKeyFilterResponseModel customerFilter;
	private Context mContext;
	public GetOneKeyFilterSettingsAsyncTask(Context mContext,MyAsyncTaskListenner myAsyncTaskListenner){
		this.mContext = mContext;
		this.myAsyncTaskListenner = myAsyncTaskListenner;
	}
	@Override
	protected void onPreExecute() {
		if(null!=myAsyncTaskListenner){
			myAsyncTaskListenner.onPreTask();
		}
	}
	@Override
	protected String doInBackground(Void... params) {
		try {
			customerFilter = ProviderFactory.createUserServiceProvider().getCustomerFilter
						(ActivityUtil.getCustomerToken(), ActivityUtil.getCustomerID());
			//保存到本地数据库
			if(null != customerFilter){
				//先清除之前保存的数据，再保存现在的数据
				long cid = ActivityUtil.getCustomerID();
				OnekeyFilterLocalUtil.deleteLocalData(mContext,cid);
				OnekeyFilterLocalUtil.saveToLocal(mContext,cid,customerFilter.getWhiteCategoryData(),true);
				OnekeyFilterLocalUtil.saveToLocal(mContext,cid,customerFilter.getBlackCategoryData(),false);
			}
		} catch (DxException e) {
			return e.getErrCode();
		}
		return "0";
	}
	@Override
	protected void onPostExecute(String result) {
		super.onPostExecute(result);
		if("0".equals(result) && null!=customerFilter){
			StringUtil.saveConfig(ActivityUtil.getCustomerID()+ServerConstant.OneKeyFilter.ISFILTERKEY_ONLINE,
					customerFilter.isFilter()?"true":"false");
		}
		if(null!=myAsyncTaskListenner && !isRelogin){
			myAsyncTaskListenner.onPostTask(result, customerFilter);
		}
	}
	@Override
	protected Context setContext() {
		return mContext;
	}
	@Override
	protected BaseNeedReLoginTask setTask() {
		return new GetOneKeyFilterSettingsAsyncTask(mContext, myAsyncTaskListenner);
	}

}
